﻿@using ZBJF.Warranty.Common.Enums.InsuranceWarranty
@using ZBJF.Warranty.WebProvider
@using ZBJF.Warranty.Common.Enums
@{
    ViewBag.Title = "Index";
}
@section Header{
    @Scripts.Render("~/bundles/datatable")
    @Styles.Render("~/Content/datatable")
}
@model ZBJF.Warranty.Model.InsuranceWarrantyRecord
<style>
    #searchForm select {
        min-width: 120px;
    }
</style>
<ul class="nav nav-tabs">
    <li class="active">
        <a href="/InsuranceWarrantyRecord/QuotationIndex">
            配件价格列表
        </a>
    </li>
    <li>
        <a href="/InsuranceWarrantyRecord/ExportQuotationList">
            创建自定义报价清单
        </a>
    </li>
</ul>
<div id="searchForm" class="breadcrumb form-search">
    <table style="text-align: right;">
        <tr>
            <td>报价类型：</td>
            <td>
                <select id="IsAvailable">
                    <option value="True">最新报价</option>
                    <option value="False">历史报价</option>
                </select>
            </td>
            <td>设备类型：</td>
            <td>
                @Html.DropDownList("ProductTypeId", ConvertToSelector.Instance.ProductTypeConvertToSelectList())
            </td>
            <td>品牌：</td>
            <td>
                @Html.DropDownList("BrandId", ConvertToSelector.Instance.ProductBrandConvertToSelectList())
            </td>
            <td>型号：</td>
            <td>
                @Html.DropDownList("ModelId", ConvertToSelector.Instance.ProductModelsConvertToSelectList())
            </td>
        </tr>
        <tr>
            <td>配件类型：</td>
            <td>
                @Html.DropDownList("AccessoriesTypeId", ConvertToSelector.Instance.AccessoriesTypeConvertToSelectList())
            </td>
            <td>服务商：</td>
            <td>
                @Html.DropDownList("ServiceProviderId", ConvertToSelector.Instance.ServiceProviderConvertToSelectList())
            </td>
            <td colspan="10" style="text-align:left">
                <input id="btnSubmit" class="btn btn-primary  Search-R margin-l-10" type="button" value="查询 " onclick="search()">

                <input id="btnImportExport" class="btn btn-success Search-R margin-l-10" type="button" value="导入报价清单" onclick="exportOpen()">
            </td>

        </tr>
    </table>
</div>
<div class="table-responsive dataTables_wrapper">
    <table id="quotationList" class="table table-striped table-bordered table-hover">
        <thead>
            <tr>
                <th width="20">序号</th>
                <th width="100">设备类型</th>
                <th width="100">品牌</th>
                <th width="100">型号</th>
                <th width="100">配件类型</th>
                <th width="100">配件等级</th>
                <th width="100">维修方式</th>
                <th width="100">服务商</th>
                <th width="60">金额</th>
                <th width="100">报价日期</th>
                <th width="100">最后修改日期</th>
                <th width="40">操作</th>
            </tr>
        </thead>
    </table>
</div>

<div class="modal fade" id="ShowEdit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="page-header">
        <h4>
            报价清单管理
            <small>
                <i class="icon-double-angle-right"></i>
                <span id="ShowText">报价编辑</span>
            </small>
        </h4>
    </div>
    <div class="form-horizontal">
        <fieldset>
            @Html.Hidden("ID")
            <div class="control-group">
                <label class="control-label">报价：</label>
                <div class="controls">
                    @Html.TextBox("QuotationPrice", "", new { @class = "col-sm-8", validate = "{required:true,messages:{required:'请输入报价'}}", maxLength = "9" })
                </div>
            </div>
        </fieldset>
        <div class="control-group">
            <div class="align-center">
                <button class="btn btn-primary Edit-R" type="submit" onclick="updatePriceBtn(this)">
                    <i class="icon-ok bigger-110"></i>
                    保存
                </button>
                <button class="btn" type="reset" data-dismiss="modal">
                    <i class="icon-undo bigger-110"></i>
                    取消
                </button>
            </div>
        </div>
    </div>
</div>
@section scripts{
<script src="~/Scripts/moment.js"></script>
    <script type="text/javascript">
        $(function () {
            var objTable = $("#quotationList")
                .dataTable({
                    fnDrawCallback: function () {
                        ControlPermissionOperate();
                    },
                    "sAjaxSource": "/InsuranceWarrantyRecord/QuotationList",
                    aoColumns: [
                        {
                            "mData": null,
                            "bSortable": false,
                            "fnRender": function (nRow) {
                                return nRow.iDataRow + 1;
                            }
                        },
                        { "mData": "ProductName", "bSortable": false },
                        { "mData": "BrandName", "bSortable": false },
                        { "mData": "ModelName", "bSortable": false },
                        { "mData": "AccessoriesName", "bSortable": false },
                        { "mData": "AccessoriesLevelName", "bSortable": false },
                        { "mData": null, "bSortable": false },
                        { "mData": "ServiceProviderName", "bSortable": false },
                        { "mData": "QuotationPrice" },
                        { "mData": "QuotationTime" },
                        { "mData": "LastModifyTime", "bSortable": false },
                        { "mData": null, "bSortable": false }
                    ],
                    "fnRowCallback": function (nRow, aData, iDisplayIndex) {
                        var html = "<a class='Edit-R' href=\"javascript:showEditBox('" + aData.ID + "'," + aData.QuotationPrice + ")\" title='编辑报价'><i class='icon-edit'></i></a>";

                        html += "<a class='Edit-R' href=\"javascript:UpdateStatus('" + aData.ID + "'," + !aData.IsAvailable + ",'/InsuranceWarrantyRecord/AccessoriesQuotationUpdateStatus','quotationList')\" title='" + (aData.IsAvailable ? "禁用" : "启用") + "'>" + (aData.IsAvailable ? bootStrapRemoveIncon : bootStrapOkIncon) + "</a>";
                        $('td:eq(11)', nRow).html(html);

                        var serviceTypeName = "";
                        //维修方式
                        if (aData.ServiceType == 1) {
                            serviceTypeName = "分拣寄修"
                        }
                        else if (aData.ServiceType == 2) {
                            serviceTypeName = "上门维修"
                        }
                        else if (aData.ServiceType == 3) {
                            serviceTypeName = "到店维修"
                        }
                        else if (aData.ServiceType == 4) {
                            serviceTypeName = "自行官修"
                        } else if (aData.ServiceType == 5) {
                            serviceTypeName = "跑腿官修"
                        } else {
                            serviceTypeName = "未知";
                        }
                        $('td:eq(6)', nRow).html(serviceTypeName);

                        $('td:eq(9)', nRow).html(moment(aData.QuotationTime).format('YYYY-MM-DD'));
                        //时间
                        $('td:eq(10)', nRow).html(moment(aData.LastModifyTime).format('YYYY-MM-DD HH:mm:ss'));
                    }
                });

            //选择设备类型事件
            $("#ProductTypeId").on("change", function () {
                var _productTypeId = $(this).val();
                if (!_productTypeId)
                    return;

                //设置配件类型
                setSelectData("/AccessoriesType/GetAccessoriesType", "GET", "AccessoriesTypeId", { ProductTypeId: _productTypeId }, "ID", "AccessoriesName", true);
                //设置品牌名称
                setSelectData("/ProductBrands/GetProductBrands", "GET", "BrandId", { ProductTypeId: _productTypeId }, "ID", "BrandName", true);
                //设置型号
                setSelectData("/ProductModels/GetProductModels", "POST", "ModelId", { ProductTypeId: _productTypeId }, "ID", "ModelName", true);
            })
            //选择品牌事件
            $("#BrandId").on("change", function () {
                var _productBrandsId = $(this).val();
                if (!_productBrandsId)
                    return;
                //设置型号
                setSelectData("/ProductModels/GetProductModels", "POST", "ModelId", { ProductBrandsId: _productBrandsId }, "ID", "ModelName", true);
            })
            //价格只能是正数 ， 保留两位小数点
            $("#QuotationPrice").on("keyup", function () {
                var price = $(this).val();
                if (price && price.substr(price.length - 1) != '.') {
                    var _price = parseInt(parseFloat(price) * 100) / 100;
                    if (_price && _price > 0)
                        $(this).val(_price);
                    else
                        $(this).val("");
                }
            })
        });

        //查询
        function search() {
            queryParamList.IsAvailable = $("#IsAvailable").val().trim();
            queryParamList.ProductTypeId = $("#ProductTypeId").val().trim();
            queryParamList.ProductBrandId = $("#BrandId").val().trim();
            queryParamList.ProductModelId = $("#ModelId").val().trim();
            queryParamList.AccessoriesTypeId = $("#AccessoriesTypeId").val().trim();
            queryParamList.ServiceProviderId = $("#ServiceProviderId").val().trim();
            $("#quotationList").dataTable().fnDraw();
        }

        //导入模板
        function exportOpen() {
            var url = '/InsuranceWarrantyRecord/ImportQuotationListForm';
            layer.open({
                type: 2,
                title: '导入报价清单',
                shadeClose: true,
                shade: 0.8,
                area: ['600px', '60%'],
                content: url //iframe的url
            });
        }

        //编辑价格
        function showEditBox(id, price) {
            if (!id || id < 0) {
                layer.alert("参数错误！");
            }
            if (!price || price < 0) {
                layer.alert("价格错误！");
            }
            $("#QuotationPrice").val(price);
            $("#ID").val(id);
            $('#ShowEdit').modal();
        }

        //获取设备类型; 数据后台地址 | 请求方式（post/get） | select标签的ID | 请求参数 | Value对应的字段名称 | Text对应的字段名称 | 是否有默认选项 true/false
        function setSelectData(url, postType, selectId, params, valueName, textName, hasDefault) {
            //加载所有的带接入列表
            $.ajax({
                type: postType,
                url: url,
                dataType: "json",
                data: params,
                success: function (data) {
                    console.log(data);
                    if (!data.Result) {
                        alert("初始化数据失败");
                        return;
                    }
                    var _data = data.Data;
                    var _html = "";
                    if (hasDefault == true) {
                        _html += "<option value='0'>请选择</option>";
                    }
                    if (_data && _data.length > 0) {
                        _data.forEach(function (item) {
                            _html += "<option value='" + item[valueName] + "'>" + item[textName] + "</option>";
                        })
                    }
                    $("#" + selectId).html(_html);
                }
            });
        }

        //修改价格
        function updatePriceBtn(_this) {
            var _data = { ID: $("#ID").val(), QuotationPrice: $("#QuotationPrice").val() };
            if (!_data.ID || _data.ID < 1) {
                layer.alert("参数错误");
                return;
            }
            if (!_data.QuotationPrice || _data.QuotationPrice < 0) {
                layer.alert("报价填写错误");
                return;
            }
            _this.disabled = 'disabled';

            $.ajax({
                type: "POST",
                url: "/InsuranceWarrantyRecord/SaveQuotationPrice",
                dataType: "json",
                data: _data,
                success: function (data) {
                    layer.alert(data.Message);
                    _this.disabled = '';
                    if (data.Result) {
                        search();
                        $('#ShowEdit').modal('hide');
                    }
                }
            });
        }

        window.onload = function () {
            search();
        }

        function reset() {
            document.getElementById("excelForm").reset();
        }
    </script>
}